package com.cheetah.start.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.cheetah.start.common.base.query.PageQuery;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.common.enums.EnableEnum;
import com.cheetah.start.module.system.domain.SysRole;
import com.cheetah.start.module.system.service.SysRoleAdminService;
import com.cheetah.start.module.system.service.SysRoleMenuService;
import com.cheetah.start.module.system.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

/**
 * 系统角色控制器
 *
 * @author Feel
 * @date 2021/09/05
 */
@Slf4j
@Valid
@Controller
@Mapping("/role")
public class SysRoleController extends ResultController {

    @Inject
    SysRoleService roleService;
    @Inject
    SysRoleMenuService roleMenuService;
    @Inject
    SysRoleAdminService roleAdminService;


    /**
     * 角色页面
     *
     * @return {@link ModelAndView}
     */
    @SaCheckPermission(value = "sys:role:main", orRole = "admin")
    @Get
    @Mapping("/view")
    public ModelAndView view() {
        return jumpPage("/role/list");
    }

    /**
     * 添加页面
     *
     * @return {@link ModelAndView}
     */
    @Get
    @Mapping("/add")
    public ModelAndView add() {
        return jumpPage("/role/add");
    }

    /**
     * 编辑页面
     *
     * @param modelAndView 模型和视图
     * @param roleId       角色id
     * @return {@link ModelAndView}
     */
    @Get
    @Mapping("/edit")
    public ModelAndView edit(ModelAndView modelAndView, Integer roleId) {
        SysRole role = roleService.getById(roleId);
        modelAndView.put("sysRole", role);
        modelAndView.view("/role/edit");
        return modelAndView;
    }


    /**
     * 角色权限页面
     *
     * @return {@link ModelAndView}
     */
    @Get
    @Mapping("/power")
    public ModelAndView power(Integer roleId, ModelAndView modelAndView) {
        modelAndView.put("roleId", roleId);
        modelAndView.view("/role/power");
        return modelAndView;
    }


    /**
     * 启用
     *
     * @param roleId 角色id
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:role:switch", orRole = "admin")
    @Post
    @Mapping("/enable")
    public Result enable(@NotNull(message = "ID不能为空") Integer roleId) {
        SysRole role = roleService.getById(roleId);
        role.setEnable(EnableEnum.ENABLE.getCode());
        boolean b = roleService.updateById(role);
        return decide(b, BusinessTypeEnum.EDIT);
    }

    /**
     * 禁用
     *
     * @param roleId 角色id
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:role:switch", orRole = "admin")
    @Post
    @Mapping("/disable")
    public Result disable(@NotNull(message = "ID不能为空") Integer roleId) {
        SysRole role = roleService.getById(roleId);
        role.setEnable(EnableEnum.DISABLE.getCode());
        if (roleService.isSuperAdminDisable(role)) {
            return failure("超级管理员不能禁用");
        }
        boolean b = roleService.updateById(role);
        return decide(b, BusinessTypeEnum.EDIT);
    }

    /**
     * 添加
     *
     * @param role 角色
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:role:add", orRole = "admin")
    @Post
    @Mapping("/save")
    public Result save(@Validated SysRole role) {
        return decide(roleService.save(role), BusinessTypeEnum.ADD);
    }

    /**
     * 更新
     *
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:role:edit", orRole = "admin")
    @Post
    @Mapping("/update")
    public Result update(@Validated SysRole role) {
        if (roleService.isSuperAdminDisable(role)) {
            return failure("超级管理员不能禁用");
        }
        return decide(roleService.updateById(role), BusinessTypeEnum.EDIT);
    }


    /**
     * 列表
     *
     * @param pageQuery 页面查询
     * @param role      角色
     * @return {@link ResultTable}
     */
    @SaCheckPermission(value = "sys:role:data", orRole = "admin")
    @Get
    @Mapping("/list")
    public ResultTable list(PageQuery pageQuery, SysRole role) {
        return roleService.getPage(pageQuery, role);
    }

    /**
     * 删除
     *
     * @param roleId 角色id
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:role:delete", orRole = "admin")
    @Delete
    @Mapping("/deleteOne")
    public Result delete(@NotNull(message = "ID不能为空") Integer roleId) {
        if (roleService.isSuperAdmin(roleId)) {
            return failure("超级管理员不能删除");
        }
        boolean b = roleService.removeById(roleId);
        roleMenuService.removeByRoleId(roleId);
        roleAdminService.removeByRoleId(roleId);
        return decide(b, BusinessTypeEnum.REMOVE);
    }
}
